﻿using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using HBCode.Presentation.Base;
using HBCode.Presentation.Command;

namespace HBCode.Presentation
{
    public class MainViewModel : ViewModelBase
    {
        #region Members

        private ObservableCollection<ViewModelBase> _ViewModelItems = new ObservableCollection<ViewModelBase>();
        public ObservableCollection<ViewModelBase> ViewModelItems
        {
            get
            {
                return _ViewModelItems;
            }
            set
            {
                if (_ViewModelItems == value)
                    return;
                OnPropertyChanging("ViewModelItems");
                _ViewModelItems = value;
                OnPropertyChanged("ViewModelItems");
            }
        }

        private ViewModelBase activeViewModel = null;
        /// <summary>
        /// View-model for the active pane.
        /// </summary>
        public ViewModelBase ActiveViewModel
        {
            get
            {
                return activeViewModel;
            }
            set
            {
                if (activeViewModel == value)
                    return;

                OnPropertyChanging("ActiveViewModel");
                activeViewModel = value;
                OnPropertyChanged("ActiveViewModel");
            }
        }

        private bool _isBusy = false;
        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                if (_isBusy != value)
                {
                    OnPropertyChanging("IsBusy");
                    _isBusy = value;
                    OnPropertyChanged("IsBusy");
                }
            }
        }

        #endregion

        #region Commandings

        private ICommand _moduleCommand;
        /// <summary>
        /// Command for loading modules
        /// </summary>
        public ICommand ModuleCommand
        {
            get
            {
                _moduleCommand = _moduleCommand = new DelegateCommand<string>((param) =>
                {
                    IsBusy = true;
                    LoadModule(param);
                    IsBusy = false;
                });
                return _moduleCommand;
            }
        }

        #endregion

        #region Miscellaneous

        /// <summary>
        /// Loads modules based on param from application menu
        /// </summary>
        /// <param name="param"></param>
        private void LoadModule(string param)
        {
            ViewModelBase viewModelBase = null;
            switch (param)
            {
                case "Customer":
                    viewModelBase = GetModule<CustomerViewModel>();
                    break;
                default:
                    break;
            }

            ActiveChildViewModel = viewModelBase;
        }

        /// <summary>
        /// Returns existing module if in collection or create new
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private ViewModelBase GetModule<T>() where T : ViewModelBase, new()
        {
            ViewModelBase viewModelBase = ViewModelItems.FirstOrDefault(e => e.GetType().Name.Equals(typeof(T).Name));

            if (viewModelBase == null)
            {
                viewModelBase = new T();
                viewModelBase.Parent = this;
                ViewModelItems.Add(viewModelBase);
            }
            return viewModelBase;
        }

        /// <summary>
        /// Called when the application is closing.
        /// Return 'true' to allow application to exit.
        /// </summary>
        public bool OnApplicationClosing()
        {
            //if (this.AnyDocumentIsModified)
            //{
            //    if (!this.DialogProvider.QueryCloseApplicationWhenDocumentsModified())
            //    {
            //        //
            //        // User has cancelled application exit.
            //        //
            //        return false;
            //    }
            //}

            //
            // Allow application exit to proceed.
            //
            return true;
        }

        #endregion
    }
}
